package bb.lanxing.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import androidx.annotation.NonNull;

import org.litepal.LitePal;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.activity.base.BaseActivity;
import bb.lanxing.model.database.City;
import bb.lanxing.model.database.Province;
import bb.lanxing.model.database.User;
import bb.lanxing.network.BiCiCallback;
import bb.lanxing.network.MyHttpClient;
import bb.lanxing.util.Log;

public class CityChooseActivity extends BaseActivity {
    private List<City> cityList;
    private Spinner citySpinner;
    private TextView cityText;
    private View mConfirmBtn;
    private List<Province> provinceList;
    private Spinner provinceSpinner;
    private TextView provinceText;
    private String oldProvince = "";
    private String oldCity = "";
    private int selectPosition = -1;

    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_city_choose);
        bindView(getWindow().getDecorView());
        setupActionBar(true);
        setTitle("选择地区");
        if (Province.getCount() <= 0) {
            Province.importDatas();
            City.importDatas();
        }
        User signinUser = App.getContext().getSigninUser();
        oldProvince = signinUser.getProvince();
        oldCity = signinUser.getCity();
        List<Province> all = Province.getAll();
        provinceList = all;
        if (all.isEmpty()) {
            App.getContext().showMessage("导入城市信息失败。");
            finish();
        }
        String[] strArr = new String[provinceList.size()];
        provinceSpinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, strArr));
        for (int i = 0; i < provinceList.size(); i++) {
            Province province = provinceList.get(i);
            String str = oldProvince;
            if (str != null && str.equals(province.getName())) {
                selectPosition = i;
            }
            strArr[i] = province.getName();
        }
        int i2 = selectPosition;
        if (i2 >= 0) {
            provinceSpinner.setSelection(i2, true);
        }
        provinceSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {
            }

            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i3, long j) {
                selectPosition = i3;
                Province province2 = provinceList.get(i3);
                Log.d("view", "position = " + i3 + " , id = " + j + " , name = " + province2.getName() + " , " + province2.getId());
                loadCity(province2);
            }
        });
        int i3 = selectPosition;
        if (i3 >= 0) {
            loadCity(provinceList.get(i3));
        }
    }

    public void loadCity(Province province) {
        City city = LitePal.findFirst(City.class);
        if (null == city) {
            City.importDatas();
        }
        List<City> byProvinceId = City.getByProvinceId(province.getId());
        cityList = byProvinceId;
        String[] strArr = new String[byProvinceId.size()];
        int i = 0;
        for (int i2 = 0; i2 < cityList.size(); i2++) {
            String str = oldCity;
            if (str != null && str.equals(cityList.get(i2).getName())) {
                i = i2;
            }
            strArr[i2] = cityList.get(i2).getName();
        }
        citySpinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, strArr));
        citySpinner.setSelection(i, true);
    }

    public void confirmClick(View view) {
        City city = cityList.get(citySpinner.getSelectedItemPosition());
        User signinUser = App.getContext().getSigninUser();
        if (signinUser == null){
            signinUser = new User();
        }
        String province = provinceList.get(provinceSpinner.getSelectedItemPosition()).getName();
        signinUser.setProvince(province);
        signinUser.setCity(city.getName());
        signinUser.save();
        App.getContext().setSigninUser(signinUser);
        BiCiCallback callback = new BiCiCallback() {
            @Override
            public void onResponseString(@NonNull String str) {
            }
        };

        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("city", city.getName());
        hashMap.put("province", province);
        MyHttpClient.updateUserProfile(callback, hashMap);
        finish();
    }

    private void bindView(View view) {
        provinceText = view.findViewById(R.id.provinceText);
        provinceSpinner = view.findViewById(R.id.provinceSpinner);
        cityText = view.findViewById(R.id.cityText);
        citySpinner = view.findViewById(R.id.citySpinner);
        View findViewById = view.findViewById(R.id.confirmBtn);
        mConfirmBtn = findViewById;
        findViewById.setOnClickListener(this::confirmClick);
    }
}